import { IPSDEDBTable } from './ipsdedbtable';
import { IPSSysDBTable } from './ipssys-dbtable';
import { IPSDataEntity } from '../dataentity/ipsdata-entity';
import { IPSDEField } from '../dataentity/defield/ipsdefield';
import { PSModelObjectImpl } from '../psmodel-object-impl';

export class PSDEDBTableImpl extends PSModelObjectImpl implements IPSDEDBTable {
  protected allpsdefields: IPSDEField[] | null = null;

  getAllPSDEFields(): IPSDEField[] | null {
    if (this.allpsdefields == null) {
      const value = this.M.getAllPSDEFields;
      if (value == null) {
        return null;
      }
      const arrayNode: any[] = value;
      if (arrayNode.length == 0) {
        return null;
      }
      const list: IPSDEField[] = [];
      const ipsdataentity = this.getParentPSModelObject('dataentity.IPSDataEntity') as IPSDataEntity;
      for (let i = 0; i < arrayNode.length; i++) {
        const item = ipsdataentity.findPSDEField(arrayNode[i]);
        if (item != null) {
          list.push(item);
        }
      }
      this.allpsdefields = list;
    }
    return this.allpsdefields.length == 0 ? null : this.allpsdefields;
  }

  get allPSDEFields(): IPSDEField[] | null {
    return this.getAllPSDEFields();
  }

  findPSDEField(objKey: any): IPSDEField | null {
    return this.getPSModel5('dataentity.defield.IPSDEField', this.getAllPSDEFields(), objKey) as IPSDEField;
  }

  get codeName(): string {
    return this.M.codeName;
  }

  get dynaModelFilePath(): string {
    return this.M.dynaModelFilePath;
  }

  get extendMode(): 0 | 2 {
    return this.M.extendMode != null ? this.M.extendMode : 0;
  }

  get memo(): string {
    return this.M.memo;
  }

  get name(): string {
    return this.M.name;
  }

  protected pssysdbtable: IPSSysDBTable | null = null;

  getPSSysDBTable(): IPSSysDBTable | null {
    if (this.pssysdbtable != null) return this.pssysdbtable;
    const value = this.M.getPSSysDBTable;
    if (value == null) {
      return null;
    }
    const ipsdataentity = this.getParentPSModelObject('dataentity.IPSDataEntity') as IPSDataEntity;
    if (ipsdataentity != null) {
      this.pssysdbtable = ipsdataentity.getPSSysDBSchemeMust().findPSSysDBTable(value);
    }
    return this.pssysdbtable;
  }

  get psSysDBTable(): IPSSysDBTable | null {
    return this.getPSSysDBTable();
  }

  getPSSysDBTableMust(): IPSSysDBTable {
    const value = this.getPSSysDBTable();
    if (value == null) {
      throw new Error('未指定关系数据库表');
    }
    return value;
  }

  get tableType(): 'MAIN' | 'USER' | 'USER2' {
    return this.M.tableType;
  }

  get userCat(): string {
    return this.M.userCat;
  }

  get userTag(): string {
    return this.M.userTag;
  }

  get userTag2(): string {
    return this.M.userTag2;
  }

  get userTag3(): string {
    return this.M.userTag3;
  }

  get userTag4(): string {
    return this.M.userTag4;
  }

  get cls(): string {
    return 'PSDEDBTableImpl';
  }

  instanceof(cls: string): boolean {
    if (cls == 'database.IPSDEDBTable') return true;
    return super.instanceof(cls);
  }
}
